<?php
/**
 * 标签管理
 */

namespace backend\modules\information\controllers;


use backend\component\BackendBaseController;
use backend\service\TagService;
use common\component\Pinyin;
use common\models\info\Tags;
use common\models\info\TagsRelation;

class TagController extends BackendBaseController
{
    public function actions()
    {
        return [
            "modify" => [
                'class' => "backend\actions\ModifyAction",
                'modelName'=> Tags::className()
            ],
            "delete" =>[
                'class' => "backend\actions\DeleteAction",
                'modelName'=> Tags::className(),
                'is_physics' => true
            ],
            "switch" => [
                "class" => "backend\actions\SwithAction",
                "modelName" => Tags::className(),
                "field" => "status",
            ],
        ];
    }
    /*
     * 标签列表
     */
    public function actionList() {
        if (\Yii::$app->request->getIsAjax()) {
            return TagService::tagsList(\Yii::$app->request->post());
        }
        $model = new Tags();
        return $this->render("list",['model'=>$model]);
    }


    public function actionEdit($id = 0) {
        $model = Tags::modelById($id);
        return $this->render("edit",["model"=>$model]);
    }

    /*
     * 关联的文章
     */
    public function actionArtotal() {
        if (\Yii::$app->request->getIsAjax()) {
            $_POST['tag_id'] = \Yii::$app->request->get("tag_id");
            return TagService::articleRelationList(\Yii::$app->request->post());
        }
        $model = new TagsRelation();
        $tag_id = \Yii::$app->request->get("tag_id");
        return $this->render("artlist",['model'=>$model,'tag_id'=>$tag_id]);
    }

    /**
     * 解除话题对应的文章
     */
    public function actionArtrelieve() {
        $object_id = \Yii::$app->request->get("object_id");
        $tag_id    = \Yii::$app->request->get("tag_id");
        $type = TagsRelation::TYPE_ARTICLE;
        $result = TagService::artRelieve($object_id,$tag_id,$type);
        return $result;
    }
}